package com.ruoyi.project.gxgw_modules.level.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.gxgw_modules.level.domain.Level;
import com.ruoyi.project.gxgw_modules.level.service.ILevelService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 会员等级 信息操作处理
 * 
 * @author ruoyi
 * @date 2019-01-19
 */
@Controller
@RequestMapping("/gxgw_modules/level")
public class LevelController extends BaseController
{
    private String prefix = "gxgw_modules/level";
	
	@Autowired
	private ILevelService levelService;
	
	@RequiresPermissions("gxgw_modules:level:view")
	@GetMapping()
	public String level()
	{
	    return prefix + "/level";
	}
	
	/**
	 * 查询会员等级列表
	 */
	@RequiresPermissions("gxgw_modules:level:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Level level)
	{
		startPage();
        List<Level> list = levelService.selectLevelList(level);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出会员等级列表
	 */
	@RequiresPermissions("gxgw_modules:level:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Level level)
    {
    	List<Level> list = levelService.selectLevelList(level);
        ExcelUtil<Level> util = new ExcelUtil<Level>(Level.class);
        return util.exportExcel(list, "level");
    }
	
	/**
	 * 新增会员等级
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存会员等级
	 */
	@RequiresPermissions("gxgw_modules:level:add")
	@Log(title = "会员等级", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Level level)
	{		
		return toAjax(levelService.insertLevel(level));
	}

	/**
	 * 修改会员等级
	 */
	@GetMapping("/edit/{levelId}")
	public String edit(@PathVariable("levelId") Integer levelId, ModelMap mmap)
	{
		Level level = levelService.selectLevelById(levelId);
		mmap.put("level", level);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存会员等级
	 */
	@RequiresPermissions("gxgw_modules:level:edit")
	@Log(title = "会员等级", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Level level)
	{		
		return toAjax(levelService.updateLevel(level));
	}
	
	/**
	 * 删除会员等级
	 */
	@RequiresPermissions("gxgw_modules:level:remove")
	@Log(title = "会员等级", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{

		if(levelService.selectIfIdForVip(ids))
			return error("等级字段被占用，删除失败");

		return toAjax(levelService.deleteLevelByIds(ids));
	}
	
}
